<?php
/*
 * Copyright (c) Madbit 2012. All Rights Reserved.
 * Reproduction in whole or in part is prohibited without the
 * written consent of the copyright owner.
 */

/**
 * Description of CategoryController
 *
 * @author Alessio Fiore
 */
class CategoryController {

    public function createCategory($idc, $parent_dir, $name) {
       
        include_once '../utils/includer.php';
        includeFileOnce("utils/directories.php");
        includeFileOnce("utils/redirect.php");
        includeFileOnce("protected/database/MySqlDAO.php");
        includeFileOnce("protected/messages/it/messages.php");

        $messages = getBundleMessages();
        
        if (!empty($_GET['idc'])) {
            $idc = $_GET['idc'];

            if (isset($_GET['parent_dir']))
                $parent_dir = $_GET['parent_dir'];
            else
                $parent_dir = 1;

            if (isset($_POST['name'])) {
                $name = $_POST['name'];

                $response = array();

                $mySqlDAO = new MySqlDAO();

                /* Check if the category already exist in the same path */
                $idcres = $mySqlDAO->checkCategoryName($idc, $name);
                if (empty($idcres)) {
                    $target_path = $mySqlDAO->getCategoryPath($idc);

                    $path = getRootDir() . $target_path . $name . DIRECTORY_SEPARATOR;

                    $old = umask(0);
                    mkdir($path, 0777) or die("ERROR: Cannot make dir " . $path);
                    umask($old);

                    $dbPath = $target_path . $name . DIRECTORY_SEPARATOR;
                    $res = $mySqlDAO->createCategory($name, $dbPath, $idc);

                    if (empty($res)) {
                        rmdir($path);
                        $response['status'] = "FAILURE";
                        $response['message'] = $messages['createcategory.error1'];
                        return $response;
                    } else {
                        redirect("index.php?idc=$idc", FALSE);
                    }
                } else {
                    $response['status'] = "FAILURE";
                    $response['message'] = $messages['createcategory.name_already_used'];
                    return $response;
                }
            } else {
                $response['status'] = "FAILURE";
                $response['message'] = $messages['createcategory.missing_data'];
                return $response;
            }
        } else {
            die($messages['admin.missed_parentdir']);
        }
    }

    public function modifyCategory($idc, $parent_dir, $name) {
        include_once '../utils/includer.php';
        includeFileOnce("utils/directories.php");
        includeFileOnce("utils/redirect.php");
        includeFileOnce("protected/database/MySqlDAO.php");
        includeFileOnce("protected/messages/it/messages.php");
        
        $messages = getBundleMessages();
        
        $mySqlDAO = new MySqlDAO();

        if (isset($idc) && isset($parent_dir)) {
            if (isset($name)) {
                $name = $_POST['name'];

                // Controllo se esiste una categoria con lo stesso nome nella parent_dir
                $idcres = $mySqlDAO->checkCategoryName($parent_dir, $name);
                if (!empty($idcres)) {
                    $response['status'] = "FAILURE";
                    $response['message'] = $messages['createcategory.name_already_used'];
                    return $response;
                } else {

                    $oldDBPath = $mySqlDAO->getCategoryPath($idc);
                    $oldPath = getRootDir() . $oldDBPath;
                    $oldname = basename($oldPath);
                    $newPath = substr($oldPath, 0, $oldPath . strlen($oldPath) - strlen($oldname . DIRECTORY_SEPARATOR)) . $name . DIRECTORY_SEPARATOR;

                    $newDBPath = substr($oldDBPath, 0, strlen($oldDBPath) - strlen($oldname . DIRECTORY_SEPARATOR)) . $name . DIRECTORY_SEPARATOR;

                    $mySqlDAO->modifyCategory($idc, $name, $newDBPath, $parent_dir);
                    rename($oldPath, $newPath);
                    redirect("index.php?idc=$parent_dir", FALSE);
                }
            } else {
                $response['status'] = "FAILURE";
                $response['message'] = $messages['createcategory.missing_data'];
                return $response;
            }
        } else {
            die($messages['admin.missed_parentdir']);
        }
    }

}
?>
